﻿module Yacc

open System.IO
open System
open System.Text

//9-5+2 to 95-2+
type InfixToPostfix(input :String) =

    let mutable index = 0
    let mutable lookahead = input.[index] 
    let output = new StringBuilder()

    member this.Term() =
        if lookahead >= '0' && lookahead <= '9' then 
            output.Append(lookahead) |>ignore
            this.Match lookahead
        else failwith "Term not digit."

    member this.Match(c :char )=
        if lookahead = c then 
                index <- index+1
                if index <input.Length then 
                    lookahead <- input.[index]  
        else
            failwith ("match error on "+ c.ToString())

    member this.Rest() = //9-5+2  95-2+
        match lookahead with
        | '+' as op -> 
            this.Match(op)
            this.Term()
            output.Append(op) |> ignore
            this.Rest()
        | '-' as op-> 
            this.Match(op)
            this.Term()
            output.Append(op)  |>ignore      
            this.Rest()
        | _ -> ()
       
    member this.Expr() =
        this.Term()
        this.Rest()
    member this.Output =
        this.Expr()
        output.ToString()
